<template>
<div style="float: left">
    <div id="box">
        <div class="yulan">
            <img v-if="this.fileList.length>0" :src="this.fileList[0].url" alt />
            <div class="aggregate" v-if="this.fileList.length>0">{{this.length}}</div>
            <div class="zhe" v-if="this.fileList.length>0">
                <div class="tishiw" v-if="this.fileList.length>1" @click="dakai">
                    <p>点击预览</p>
                </div>
                <div class="tishiw" v-else>
                    <span class="el-icon-zoom-in" @click="dakai"></span>
                    <span class="el-icon-delete" @click="shanchu"></span>
                </div>
            </div>
        </div>
        <div class="clear box2">
            <div class="xcname">{{xinxi.name}}</div>
            <div class="xcname">最多上传{{xinxi.limit}}张</div>
        </div>
        <div class="ananniu">
            <el-upload class="upload-demo" action :auto-upload="false" :file-list="fileList" :list-type="xinxi.listType" :limit="xinxi.limit" :on-change="aaa" ref="hc" :http-request="chuanchuan" multiple :name="xinxi.ziduan" :show-file-list="false" :on-exceed="exceed">
                <el-button slot="trigger" size="small" type="primary" class="bianju">选择照片</el-button>
                <el-button size="small" :disabled="inputState" type="success" @click="anniu" class="bianju">点击上传</el-button>
            </el-upload>
            <div></div>
        </div>
    </div>
    <xiagndialog :state="diaState" :fileList="fileList" @guanbi="guanbi" @shanchu="shanchu"></xiagndialog>
</div>
</template>

<script>
import xiagndialog from "./xiangDialog";
// import * as api from "../utils/api";
export default {
    name: "photo",
    data() {
        return {
            fileList: [],
            diaState: false,
            myFormData: "",
        };
    },
    props: ["xinxi"],
    components: {
        xiagndialog,
    },
    computed: {
        // 做上传判断的
        inputState() {
            // console.log("相册", 111);
            if (this.fileList.length > 0) {
                console.log("xinagce");
                // this.$emit(this.xinxi.fun, "1");
                return false;
            } else {
                // this.$emit(this.xinxi.fun, "0");
                return true;
            }
        },
    },
    methods: {
        exceed(files, fileList) {
            console.log(files);
            this.$message.warning(
                `当前限制选择 ${this.xinxi.limit} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
            );
            // this.$message.success("上传成功");
            // if (this.xinxi.fun) {
            //     this.$emit(this.xinxi.fun, response);
            // }
        },
        aaa(file, fileList) {
            this.fileList = fileList;
            // console.log(this.fileList);
            this.url = fileList[0].url;
            this.length = fileList.length;
        },
        dakai() {
            this.diaState = true;
        },
        guanbi() {
            this.diaState = false;
        },
        shanchu(item) {
            if (this.fileList.length > 1) {
                for (let i = 0; i < this.fileList.length; i++) {
                    console.log(73);
                    if (this.fileList[i].uid === item.uid) {
                        console.log(79);
                        this.fileList.splice(i, 1);
                        this.length = this.fileList.length;
                        console.log(this.fileList);
                    }
                }
            } else {
                this.fileList.splice(0, 1);
                this.length = this.fileList.length;
            }
        },
        anniu() {
            console.log("按钮");
            this.myFormData = new FormData();
            this.$refs.hc.submit();
            // 可以打印fromdata
            // for (var value of this.myFormData.values()) {
            //     console.log(value);
            // }
            this.axios({
                method: "post",
                url: this.xinxi.jiekou,
                data: this.myFormData,
            }).then((data) => {
                // console.log(data);
                if (data.data.code === 200) {
                    // this.$message.success("上传成功");
                    if (this.xinxi.fun) {
                        this.$emit(this.xinxi.fun, data);
                    }
                } else {
                    this.$message.error(`${this.xinxi.name}上传失败请重新上传`);
                }
            });
        },
        chuanchuan(file) {
            this.myFormData.append(this.xinxi.ziduan, file.file);
        },
    },
};
</script>

<style lang="less">
.el-button+.el-button {
    margin-left: 0;
}
</style><style lang="less" scoped>
#box {
    width: 200px;
    height: 250px;
    border: 1px dashed #c0ccda;
    padding-top: 8px;
}

.yulan {
    width: 180px;
    height: 150px;
    margin: auto;
    background-image: url("../assets/xiang.png");
    background-repeat: no-repeat;
    background-position: center center;
    background-color: rgba(255, 255, 255, 0.89);
    cursor: pointer;
    // box-shadow: 0px 0px 4px #000000;
    // box-shadow: 0px 0px 4px #000000;
    // box-shadow: 0px 0px 4px #000000;
    // border: 1px solid #999;
    overflow: hidden;
    position: relative;

    img {
        height: 100%;
        margin: 0 50%;
        transform: translateX(-50%);
    }
}

.ananniu {
    width: 100%;
    // height: 40px;
    text-align: center;
    float: left;
    margin-top: 16px;
    position: relative;
}

.aggregate {
    font-size: 30px;
    color: #000;
    position: absolute;
    bottom: 2px;
    right: 8px;
}

.xcname {
    font-size: 12px;
    width: 100%;
    // float: left;
    color: #999;
    text-align: center;
    // padding-left: 8px;
    box-sizing: border-box;
}

.box2 {
    margin-top: 8px;
}

.zhe {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    position: absolute;
    top: 0;
    left: 0;
    display: none;
}

.tishiw {
    width: 80px;
    height: 26px;
    line-height: 26px;
    color: #fff;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    margin: auto;

    p {
        text-align: center;
    }

    span {
        font-size: 26px;
        color: #fff;
        margin: 0 5px;
    }
}

.yulan:hover .zhe {
    display: block;
}

.bianju {
    margin: 0 5px;
}

.bianju2 {
    position: absolute;
    bottom: 0;
    right: 0;
    height: 32px;
    width: 80px;
}
</style>
